package com.zhiyan.say.webservice.rest;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.zhiyan.common.util.model.DataModel;
import com.zhiyan.common.util.model.Page;
import com.zhiyan.common.web.SecurityContextHolder;
import com.zhiyan.say.domain.entity.Artwork;
import com.zhiyan.say.service.ArtworkService;
import com.zhiyan.security.account.domain.entity.Account;

@RestController
@RequestMapping(value = "/say/artwork/rest")
public class ArtworkRestController {
	private static Logger logger = Logger.getLogger(ArtworkRestController.class);
	
	@Autowired
	private ArtworkService artworkService;
	
	/**
	 * 保存分类信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public DataModel save(HttpServletRequest request, @RequestBody Artwork artwork){
		DataModel dataModel = new DataModel();
		try {
			Account account = SecurityContextHolder.getUser();
			if (account==null) {
				dataModel.setFail();
				dataModel.setMsg("用户未登录");
				return dataModel;
			}else{
				if (artwork.getId()==null || artwork.getId().length()<=0) {	//新增
					artwork.setAccountId(account.getId());
					artwork.setCreateDate(new Date());
					artwork.setStatus("0");
					artworkService.add(artwork);
				}else{	//更新
					artworkService.update(artwork);
				}
			}
			dataModel.setSucc();
		} catch (Exception e) {
			dataModel.setFail();
			logger.error(e);
		}
		return dataModel;
	}
	
	/**
	 * 分页查询
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryPage", method=RequestMethod.POST)
	public DataModel queryPage(HttpServletRequest request, String classificationId, Integer pageNo, Integer pageSize){
		DataModel dataModel = new DataModel();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("classificationId", classificationId);
			if (pageNo==null) pageNo=1;
			if (pageSize==null) pageSize=10;
			Page page = new Page(pageNo,pageSize);
			List<Artwork> accountList = artworkService.queryPage(page, map);
			dataModel.setPageData(page, accountList);
		} catch (Exception e) {
			dataModel.setFail();
			logger.error(e);
		}
		return dataModel;
	}
	
	/**
	 * 条件查询
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/query", method=RequestMethod.POST)
	public DataModel query(HttpServletRequest request){
		DataModel dataModel = new DataModel();
		try {
			String classificationId = request.getParameter("classificationId");
			Map<String, Object> map = new HashMap<String, Object>();
			if (!StringUtils.isEmpty(classificationId)) map.put("classificationId", classificationId);
			List<Artwork> accountList = artworkService.query(map);
			dataModel.setData(accountList);
		} catch (Exception e) {
			dataModel.setFail();
			logger.error(e);
		}
		return dataModel;
	}
	
	/**
	 * 根据ID获取数据
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/getBy/{id}", method=RequestMethod.GET)
	public DataModel getById(@PathVariable String id){
		DataModel dataModel = new DataModel();
		try {
			Artwork account = artworkService.getById(id);
			if (account==null) {
				dataModel.setFail("数据不存在");
			}else{				
				dataModel.setContent(account);
			}
		} catch (Exception e) {
			dataModel.setFail();
			logger.error(e);
		}
		return dataModel;
	}
	
}
